﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;
using PeajesWeb.Models;
using System.Text;

namespace PeajesWeb.Controllers
{
    public class EmpleadoController : Controller
    {
        private string empleadoRESTServices = "http://localhost:17279/Empleados.svc/Empleados";
        JavaScriptSerializer js = new JavaScriptSerializer();
       
        //
        // GET: /Empleado/

        public ActionResult Index()
        {
            HttpWebRequest req5 = (HttpWebRequest)WebRequest.Create(empleadoRESTServices);
            req5.Method = "GET";
            HttpWebResponse res5 = (HttpWebResponse)req5.GetResponse();
            StreamReader reader5 = new StreamReader(res5.GetResponseStream());
            string listaJson = reader5.ReadToEnd();
            List<Empleado> empleadoLista = js.Deserialize<List<Empleado>>(listaJson);

            return View(empleadoLista);
        }

        //
        // GET: /Empleado/Details/5

        public ActionResult Details(int id)
        {
            HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(empleadoRESTServices + "/" + id);
            req2.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req2.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string empleadoJson2 = reader2.ReadToEnd();
            Empleado empleadoObtenido = js.Deserialize<Empleado>(empleadoJson2);

            return View(empleadoObtenido);
        }

        //
        // GET: /Empleado/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Empleado/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Empleado empleadoACrear = new Empleado()
                {
                    Codigo = 3,
                    DNI = collection["DNI"],
                    Nombre = collection["Nombre"],
                    Apellido = collection["Apellido"],
                    FechaNacimiento = collection["FechaNacimiento"],
                    Telefono = collection["Telefono"],
                    Estado = collection["Estado"],
                    Usuario = collection["Usuario"],
                    Clave = collection["Clave"] 
                };
                string postdata = js.Serialize(empleadoACrear);
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                //string postdata = "{\"Codigo\":\"" + collection["Codigo"] + "\",\"DNI\":\"" + collection["DNI"] + "\",\"Nombre\":\"" + collection["Nombre"] + "\",\"Apellido\":\"" + collection["Apellido"] + "\",\"FechaNacimiento\":\"" + collection["FechaNacimiento"] + "\",\"Telefono\":\"" + collection["Telefono"] + "\",\"Estado\":\"" + collection["Estado"] + "\",\"Usuario\":\"" + collection["Usuario"] + "\",\"Clave\":\"" + collection["Clave"] + "\"}"; //JSON
                //byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(empleadoRESTServices);
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();

                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error", e.Message);
            return View();
            }
        }
        
        //
        // GET: /Empleado/Edit/5
 
        public ActionResult Edit(int id)
        {
            HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(empleadoRESTServices + "/" + id);
            req2.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req2.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string empleadoJson2 = reader2.ReadToEnd();
            Empleado empleadoObtenido = js.Deserialize<Empleado>(empleadoJson2);

            return View(empleadoObtenido);
        }

        //
        // POST: /Empleado/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                Empleado empleadoAModificar = new Empleado()
                {
                    Codigo=id,
                    DNI = collection["DNI"],
                    Nombre = collection["Nombre"],
                    Apellido = collection["Apellido"],
                    FechaNacimiento = collection["FechaNacimiento"],
                    Telefono = collection["Telefono"],
                    Estado = collection["Estado"],
                    Usuario = collection["Usuario"],
                    Clave = collection["Clave"] 
                };

                string postdata = js.Serialize(empleadoAModificar);
                byte[] data3 = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req3 = (HttpWebRequest)WebRequest.Create(empleadoRESTServices);
                req3.Method = "PUT";
                req3.ContentLength = data3.Length;
                req3.ContentType = "application/json";
                var reqStream3 = req3.GetRequestStream();
                reqStream3.Write(data3, 0, data3.Length);
                HttpWebResponse res3 = (HttpWebResponse)req3.GetResponse();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Empleado/Delete/5
 
        public ActionResult Delete(int id)
        {
            HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(empleadoRESTServices + "/" + id);
            req2.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req2.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string empleadoJson2 = reader2.ReadToEnd();
            Empleado empleadoObtenido = js.Deserialize<Empleado>(empleadoJson2);

            return View(empleadoObtenido);
        }

        //
        // POST: /Empleado/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                string postdata4 = "\"" + id + "\""; //JSON
                byte[] data4 = Encoding.UTF8.GetBytes(postdata4);
                HttpWebRequest req4 = (HttpWebRequest)WebRequest.Create(empleadoRESTServices);
                req4.Method = "DELETE";
                req4.ContentLength = data4.Length;
                req4.ContentType = "application/json";
                var reqStream2 = req4.GetRequestStream();
                reqStream2.Write(data4, 0, data4.Length);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
